package com.vcredit.creditcat.web;

import com.vcredit.creditcat.enumtype.ProductOnlineStatusEnum;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.model.ProductBusinessStatus;
import com.vcredit.creditcat.redis.RedisServiceExt;
import com.vcredit.creditcat.service.MerchantProductService;
import com.vcredit.creditcat.service.ProductBusinessStatusService;
import com.vcredit.creditcat.vo.BuiSelectVo;
import com.vcredit.creditcat.vo.MerchantProductVo;
import com.vcredit.creditcat.vo.MerchantProductVoExt;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/*
 * @author zhangxingong
 * @version 1.0
 * @since 1.0
*/
@RestController
@RequestMapping("/merchantProduct")
public class MerchantProductController extends BaseController {

  @Autowired
  private MerchantProductService merchantProductService;

  @Autowired
  private ProductBusinessStatusService productBusinessStatusService;

  @Autowired
  private RedisServiceExt redisServiceExt;

  @RequestMapping("/list")
  public ModelAndView showPageMerchantProduct(HttpServletRequest request) {
    return new ModelAndView("/merchantproduct/merchantProductList");
  }

  @RequestMapping("/listMerchantProduct")
  public Page<MerchantProduct> findByPageMerchantProduct(HttpServletRequest request) {
    return merchantProductService.findByPage(makeDefaultCriteria(request));
  }

  @RequestMapping("/listAllProduct")
  public List<BuiSelectVo> listAllProduct(HttpServletRequest request) {
    return merchantProductService.listAllProduct();
  }

  @RequestMapping("/saveMerchantProduct")
  public String saveMerchantProduct(@RequestBody MerchantProductVo merchantProductVo,
      HttpServletRequest request) {
    merchantProductService.save(merchantProductVo);
    redisServiceExt.del("MerchantProduct");
    return SUCCESS;
  }

  @RequestMapping("/saveMerchantProductExt")
  public String saveMerchantProductExt(@RequestBody MerchantProductVoExt merchantProductVoExt) {
    merchantProductService.saveExt(merchantProductVoExt);
    return SUCCESS;
  }

  @RequestMapping("/updateMerchantProduct")
  @Transactional
  public String merchantProductUpdate(@RequestBody MerchantProduct merchantProduct,
      HttpServletRequest request) {
    merchantProductService.update(merchantProduct);
    saveProductBusinessStatus(merchantProduct);
    return SUCCESS;
  }

  @RequestMapping("/removeMerchantProduct")
  public String removeMerchantProduct(@RequestParam("ids") String ids, HttpServletRequest request) {
    merchantProductService.removeMerchantProduct(ids);
    return SUCCESS;
  }
  @ResponseBody
  @RequestMapping("/listAllOnlineStatus")
  public List<BuiSelectVo> listAllOnlineStatus(HttpServletRequest request) {
    List<BuiSelectVo> onLineStatusList=new ArrayList<>();
    for(ProductOnlineStatusEnum onlineStatusEnum:ProductOnlineStatusEnum.values()){
      BuiSelectVo item=new BuiSelectVo();
      item.setText(onlineStatusEnum.getDesc());
      item.setValue(String.valueOf(onlineStatusEnum.getCode()+1));
      onLineStatusList.add(item);
    }
    return onLineStatusList;
  }

  /**
   * 保存产品商务状态信息
   * @param merchantProduct
   */
  private void saveProductBusinessStatus(MerchantProduct merchantProduct){
    ProductBusinessStatus productBusinessStatus=new ProductBusinessStatus();
    if(merchantProduct.getOnlineStatus()!=null){
      productBusinessStatus.setOnlineStatus(merchantProduct.getOnlineStatus()-1);
    }
    productBusinessStatus.setProductBusinessId(merchantProduct.getProductBusinessId());
    productBusinessStatus.setProductId(merchantProduct.getProductId());
    productBusinessStatus.setBusinessStartDate(merchantProduct.getBusinessStartDate());
    productBusinessStatus.setContractStartDate(merchantProduct.getContractStartDate());
    productBusinessStatus.setContractEndDate(merchantProduct.getContractEndDate());
    productBusinessStatus.setPrepareOnlineDate(merchantProduct.getPrepareOnlineDate());
    productBusinessStatus.setOnlineStartDate(merchantProduct.getOnlineStartDate());
    productBusinessStatus.setOnlineEndDate(merchantProduct.getOnlineEndDate());
    productBusinessStatus.setRemarks(merchantProduct.getRemarks());
    if(productBusinessStatus!=null && productBusinessStatus.getProductBusinessId()!=null){
      productBusinessStatusService.update(productBusinessStatus);
    }else{
      ProductBusinessStatus productBusinessStatus1=productBusinessStatusService.findByProductId(merchantProduct.getProductId());
      if(productBusinessStatus1!=null){
        productBusinessStatus.setProductBusinessId(productBusinessStatus1.getProductBusinessId());
        productBusinessStatusService.update(productBusinessStatus);
      }else{
        productBusinessStatusService.save(productBusinessStatus);
      }
    }
  }
}
